package com.app.nba.web;

import com.app.nba.pojo.LoginVO;
import com.app.nba.pojo.User;
import com.app.nba.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/user")
public class LoginController {

    @Autowired
    private UserService userService;

    @PostMapping("/login")
    public String userLogin(HttpSession session, Model model, LoginVO user) {
        // 准备数据
        String msg = this.userService.login(user);

        // 登录失败
        if (!"OK".equals(msg)) {
            // 传输数据给前端
            model.addAttribute("msg", msg);

            return "message";
        }

        // 登录成功
        session.setAttribute("user", user);

        model.addAttribute("msg", "登录成功");
        return "message";

    }
    @PostMapping("/register")
    public String userRegister(HttpSession session, Model model, LoginVO user) {
        // 准备数据
        String msg = this.userService.register(user);

        // 注册失败
        if (!"OK".equals(msg)) {
            // 传输数据给前端
            model.addAttribute("msg", msg);

            return "message";
        }

        // 注册成功
        session.setAttribute("user", user);

        model.addAttribute("msg", "注册成功");
        return "message";

    }
}
